<script setup>
import {reactive} from "vue";
import {requestGetInfo, requestSetInfo} from "@/api";

const storageGroup = reactive({
  userinfo:uni.getStorageSync("userInfo"),
  initUserInfo(){
    if (this.userinfo.login_state){
      userInfoGroup.getUserInfo(this.userinfo.account);
    }
  }
})

const beforeClose = (action) =>
    new Promise((resolve) => {
      if (action === 'confirm') {
        resolve(true);
        dialogGroup.show = false;
      } else {
        // 拦截取消操作
        resolve(false);
      }
    })

const dialogGroup = reactive({
  title: "个人信息",
  message: "默认",
  show: false,
})

const userInfoGroup = reactive({
  username:"",
  account:"",
  sex:"",
  age:0,
  introduce:"",
  async getUserInfo(account){
    let result = await requestGetInfo({account:account});
    if (result.statusCode === 200){
      this.username = result.data[0].username;
      this.account = result.data[0].account;
      this.sex = result.data[0].sex;
      this.age = result.data[0].age;
      this.introduce = result.data[0].introduce;
    }
  },
  async setUserInfo(){
    let result = await requestSetInfo({username:this.username,account:storageGroup.userinfo.account,sex:this.sex,age:this.age,introduce:this.introduce});
    if (result.statusCode === 200) {
      if (result.data === "DataFormatError") {
        dialogGroup.message = "个人信息格式错误";
      } else if (result.data === "EmptyError") {
        dialogGroup.message = "个人信息为空";
      } else if (result.data === "Normal") {
        dialogGroup.message = "修改成功";
      } else if (result.data === "DatabaseError") {
        dialogGroup.message = "数据库错误";
      } else if (result.data === "DataNotFoundErrorError") {
        dialogGroup.message = "账户不存在";
      } else {
        dialogGroup.message = "服务器错误，请联系管理员";
      }
    }
    dialogGroup.show = true;
  },
})

storageGroup.initUserInfo();
</script>

<template>
  <van-dialog
      :before-close="beforeClose"
      :message="dialogGroup.message"
      :show="dialogGroup.show"
      :title="dialogGroup.title"
      custom-class="my-custom-class"
  />
  <view class="user-info">
    <view class="user-info-box">
      <van-cell-group>
        <van-field
            :value="userInfoGroup.username"
            label="用户名"
            placeholder="请输入用户名"
            input-align="right"
            @change="userInfoGroup.username = $event.detail"
        />
        <hr />
        <van-field
            :value="userInfoGroup.account"
            disabled
            label="账号"
            placeholder="wdnmd卡bug是吧"
            input-align="right"
        />
        <hr />
        <van-field
            :value="userInfoGroup.sex"
            label="性别"
            placeholder="请输入您的性别"
            input-align="right"
            @change="userInfoGroup.sex = $event.detail"
        />
        <hr />
        <van-field
            :value="userInfoGroup.age"
            label="年龄"
            placeholder="请输入您的性别"
            input-align="right"
            @change="userInfoGroup.age = $event.detail"
        />
        <hr />
        <van-field
            :value="userInfoGroup.introduce"
            label="个人介绍"
            placeholder="请输入您的个人介绍"
            input-align="right"
            @change="userInfoGroup.introduce = $event.detail"
        />
      </van-cell-group>
    </view>
    <view class="user-info-btn">
      <button @click="userInfoGroup.setUserInfo()">保存</button>
    </view>
  </view>
</template>

<style scoped>

</style>